
import {lift} from "./util.js";
import {defineRadioButtons} from "./buttons.js";
import {defineFocusNav} from "./focusNav.js";
import {defineRovingTabindexByHandle} from "./rovingTabindex.js";

/**
 * Implements WAI-ARIA Radio Group pattern: https://www.w3.org/TR/wai-aria-practices/#radiobutton
 *
 * @param radiogroup d3 selection of one or more radiogroup containers
 * @param {Handle} handle the field in the backing store that stores the name of the selected radio item
 * @param {Function?} fn optional callback ƒ(name, meta) to perform the store update, passing in the name of clicked
 *        button and the meta object to use when mutating the store. When not specified, the default behavior is to
 *        store the name in the handle directly.
 */
export function defineRadiogroup(radiogroup, handle, fn) {
    radiogroup.each(lift(group => {
        const buttons = group.selectAll("[role=radio]");
        defineRadioButtons(buttons, handle, fn);
        defineFocusNav(buttons, {enableUpDown: true});
        defineRovingTabindexByHandle(buttons, handle);
    }));
}
